// pages/goods-specs/index.js
import {
    ReDistributionMall
} from "../../api/shipping.js"
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        // 是否显示
        showBool: {
            type: Boolean,
            value: false
        },
        // 按钮文字
        btnText: {
            type: String,
            value: ''
        },
        // 详情数据
        dataObj: {
            type: Object,
            value: {}
        },
        // 详情类型
        detailType: {
            type: String,
            value: '',
        }
    },
    pageLifetimes: {
        show: function() {
          // 页面显示  
        },
        hide: function() {
          // 页面被隐藏
        },
        resize: function(size) {
          // 页面尺寸变化
        }
      },
      lifetimes: {
        attached() {
            this.unifiedFormat()
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        active: 0,
        activeChild: 0,
        goodsData:{},
        isNumShow: false,
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // 写一个辅助函数 处理 props dataObj 数据 统一格式
        unifiedFormat() {
            if (this.data.detailType === '10') {
                // 秒杀
                this.setData({
                    goodsData:{
                        ...this.data.dataObj,
                        price: this.data.dataObj.price,
                        marketprice: this.data.dataObj.marketprice,
                        name: this.data.dataObj.goodsname,
                        coverImg: this.data.dataObj.thumb,
                        property: [
                            {
                                propertyid: this.data.dataObj.propertyid,
                                propertyname: this.data.dataObj.propertyname,
                            }
                        ]
                    },
                    isNumShow: false,
                })
            } else if (this.data.detailType === '8' || this.data.detailType === '9') {
                console.log('this.data.dataObj', this.data.dataObj);
                // 8新品专区  9特卖活动
                this.setData({
                    goodsData:{
                        ...this.data.dataObj,
                        price: this.data.dataObj.price,
                        marketprice: this.data.dataObj.marketprice,
                        name: this.data.dataObj.goodsname,
                        coverImg: this.data.dataObj.thumb,
                        property: [
                            {
                                propertyid: this.data.dataObj.propertyid,
                                propertyname: this.data.dataObj.propertyname,
                            }
                        ]
                    },
                    isNumShow: false,
                })
            }else if (this.data.detailType === '1') {
                console.log('this.data.dataObj', this.data.dataObj);
                let property = this.data.dataObj.property.map(item => item.child)[0]
                property.forEach(item => item.propertyname = item.property_name)
                this.setData({
                    goodsData:{
                        ...this.data.dataObj,
                        coverImg: "http://static.chonglaoban.cn" + this.data.dataObj.thumb,
                        price: this.data.dataObj.sellprice,
                        marketprice: this.data.dataObj.marketprice,
                        property: property
                    },
                    isNumShow: false,
                })
            }
            console.log(this.data.goodsData)
        },
        /**
         * 选择规格回调函数方法
         * @param {*} e 
         */

        handleSelSpecs(e) {
            console.log(this.data.dataObj)
            const index = e.currentTarget.dataset.index;
            if (this.data.dataObj.property) {
                this.data.dataObj.propertyid = this.data.dataObj.property[index].child[this.data.activeChild].id
                this.data.dataObj.propertyname = e.currentTarget.dataset.value.property_name
                this.setData({
                    active: index,
                    activeChild: 0, // 重置第二个规格栏的选中状态
                    thumb: this.data.dataObj.property[index].child[this.data.activeChild].img,
                    property_price:this.data.dataObj.property[index].child[this.data.activeChild].price,
                });
            }

            this.triggerEvent('changeSpec', {
                propertyid: this.data.dataObj.propertyid,
                propertyname: this.data.dataObj.propertyname
            })
        },
        handleSelSpecsChild(e) {
            const index = e.currentTarget.dataset.index;
            this.setData({
                activeChild: index,
                property_price:this.data.dataObj.property[this.data.active].child[index].price
            });
            this.data.propertydata = {...e.currentTarget.dataset.value}
        },
        /**
         * 商品数量变化回调函数方法
         * @param {*} e 
         */
        // handleNumChange(e) {
        //     this.setData({
        //         count: e.detail
        //     })
        // },
        /**
         * 确认按钮回调函数方法
         */
        onConfirm() {
            this.triggerEvent('confirm', {
               
            })
        },
        /**
         * 关闭
         */
        close() {
            this.triggerEvent('close')
        }
    }
})